import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;

/*
多态
    存在条件：继承，重写，父变量引用子对象
        继承是为了父变量能引用子对象，如Teacher继承于People，则所有的Teacher都是People
        重写则可表现出不同的特点，实现多态

instanceof
    可以用来判断类

 */
/*
主人喂宠物吃食物
    主人是一个实例，喂是一个动作(方法)，
    宠物是一类的总称，吃是宠物共有的动作(方法)可表现多态，
    食物也各不相同，可表现多态

People
    姓名，喂
Animal
    吃
Food
    名称
 */
public class Polymorphism {
    public static void main(String[] args){
        People p = new People("王五");
        System.out.println("--多态测试：--\r\n\t父变量引用子对象,子对象重载父对象方法");
        p.feed(new Animal(), new Food());
        //父变量引用子对象
        p.feed(new Cat(), new Fish());
        p.feed(new Pig(), new Grass());
        p.feed(new Dog(), new Meat());

        System.out.println("instanceof 可以用来判断类");
        {
            Pig pt =  new Pig();
            if (pt instanceof Animal){
                System.out.println("Pig是Animal类");
            }
            if (pt instanceof Pig){
                System.out.println("Pig是Pig类");
            }
        }

    }
}

class People{
    String name;
    public People(){

    }
    public People(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void feed(Animal an, Food fd){
        System.out.print(name+"喂");
        an.eat(fd);
    }
}

class Food{
    public String getFood(){
        return "食物";
    }
}

class Fish extends Food{
    public String getFood(){
        return "鱼";
    }
}

class Meat extends Food{
    public String getFood(){
        return "肉";
    }
}

class Grass extends Food{
    public String getFood(){
        return "草";
    }
}

class Animal{
    public void eat(Food f){
        System.out.println("宠物吃"+f.getFood());
    }
}

class Cat extends Animal{
    public void eat(Food f){
        System.out.println("小猫吃"+f.getFood());
    }
}

class Dog extends Animal{
    public void eat(Food f){
        System.out.println("小狗吃"+f.getFood());
    }
}

class Pig extends Animal{
    public void eat(Food f){
        System.out.println("小猪吃"+f.getFood());
    }
}
